//
//  ZGLGuidePageCell.m
//  Faker
//
//  Created by dllo on 15/11/10.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "ZGLGuidePageCell.h"
#import "ZGLChannelModel.h"
#import "ZGLOptionsModel.h"
#import <UIButton+AFNetworking.h>

@interface ZGLGuidePageCell ()

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIButton *imageV;

@end

@implementation ZGLGuidePageCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.titleLabel = [[UILabel alloc] init];
        self.imageV = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.contentView addSubview:_titleLabel];
        [self.contentView addSubview:_imageV];
    }
    return self;
}
- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes {
    [super applyLayoutAttributes:layoutAttributes];
    [self.imageV setFrame:CGRectMake(ScaleX * 10, 0, layoutAttributes.frame.size.width - ScaleX * 20, layoutAttributes.frame.size.height - ScaleY * 15)];
    [self.titleLabel setFrame:CGRectMake(0, layoutAttributes.frame.size.height - ScaleY * 15, layoutAttributes.frame.size.width, ScaleY * 15)];
    self.titleLabel.font = [UIFont systemFontOfSize:12];
    self.titleLabel.textAlignment = 1;
    [self.imageV setSelected:YES];
    [self.imageV addTarget:self action:@selector(imageV:) forControlEvents:(UIControlEventTouchUpInside)];
}
- (void)imageV:(UIButton *)button {
    
    if (button.selected) {
        /* 添加 */
        self.backgroundColor = naviColor;
        NSMutableArray *arr = [NSMutableArray arrayWithCapacity:0];
        if ([_dic[@"title"] isEqualToString:@"生活"]) {
            for (NSDictionary *dic in _dic[@"sons"]) {
                if ([dic[@"title"] isEqualToString:@"生活精选"]) {
                    for (NSDictionary *dict in dic[@"sons"]) {
                        [arr addObject:dict];
                        if (arr.count == 1) {
                            break;
                        }
                    }
                }
            }
        } else {
            for (NSDictionary *dic in _dic[@"sons"]) {
                [arr addObject:dic];
                if (arr.count == 1) {
                    break;
                }
            }
        }
        [self saveNSUserDefaultsWith:arr];
    } else {
        self.backgroundColor = [UIColor clearColor];
        /* 删除 */
        NSMutableArray *arr = [NSMutableArray arrayWithCapacity:0];
        
        if ([_dic[@"title"] isEqualToString:@"生活"]) {
            for (NSDictionary *dic in [self readNSUserDefaults]) {
                if ([dic[@"title"] isEqualToString:@"情爱频道"]) {
                  
                } else {
                    [arr addObject:dic];
                }
            }
        } else {
            for (NSDictionary *dic in [self readNSUserDefaults]) {
                if ([dic[@"title"] isEqualToString:_dic[@"sons"][0][@"title"]]) {
                    
                } else {
                    [arr addObject:dic];
                }
            }
        }
   
        [self deSaveNSUserDefaultsWith:arr];
    }
    button.selected = !button.selected;
    [self.delegate isOK];
}
- (void)setDic:(NSDictionary *)dic {
    if (_dic != dic) {
        _dic = dic;
    }
    self.titleLabel.text = dic[@"title"];
    [self.imageV setImageForState:(UIControlStateNormal) withURL:[NSURL URLWithString: dic[@"list_icon"]]];
}
- (void)deSaveNSUserDefaultsWith:(NSMutableArray *)arr {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:arr forKey:@"currentArr"];
    //这里建议同步存储到磁盘中，但是不是必须的
    [userDefaults synchronize];
}

//保存数据到NSUserDefaults
- (void)saveNSUserDefaultsWith:(NSMutableArray *)arr {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    for (NSDictionary *dic in [self readNSUserDefaults]) {
        [arr addObject:dic];
    }
    [userDefaults setObject:arr forKey:@"currentArr"];
    //这里建议同步存储到磁盘中，但是不是必须的
    [userDefaults synchronize];
}
//从NSUserDefaults中读取数据
- (NSMutableArray *)readNSUserDefaults {
    NSUserDefaults *userDefaultes = [NSUserDefaults standardUserDefaults];
    NSMutableArray *myArray = [userDefaultes arrayForKey:@"currentArr"].mutableCopy;
    return myArray;
}



@end
